package com.njust.RateMyTray_backend.controller;

import com.njust.RateMyTray_backend.common.Result;
import com.njust.RateMyTray_backend.config.JwtAuthInterceptor;
import com.njust.RateMyTray_backend.service.NotificationService;
import com.njust.RateMyTray_backend.vo.NotificationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping
    public Result<List<NotificationVO>> getNotifications() {
        Long currentUserId = Long.valueOf(JwtAuthInterceptor.getCurrentUserClaims().get("userId").toString());
        log.info("用户 {} 正在获取通知列表", currentUserId);
        
        // 1. 获取用户的通知列表
        List<NotificationVO> notifications = notificationService.getNotificationsForUser(currentUserId);
        
        // 2. 将该用户的所有通知标记为已读
        notificationService.markAllNotificationsAsRead(currentUserId);
        log.info("用户 {} 的所有通知已标记为已读", currentUserId);
        
        return Result.success(notifications);
    }
} 